package com.zkthink.ceres.app.product.controller;

import com.zkthink.base.R;
import com.zkthink.base.request.PageParams;
import com.zkthink.ceres.common.annotations.LoginMode;
import com.zkthink.ceres.product.dto.SubjectProductCombineDTO;
import com.zkthink.ceres.product.entity.Subject;
import com.zkthink.ceres.product.entity.SubjectProduct;
import com.zkthink.ceres.product.service.SubjectProductService;
import com.zkthink.ceres.product.service.SubjectProductService;
import com.zkthink.ceres.product.service.SubjectService;
import com.zkthink.ceres.product.vo.ProductPageVO;
import com.zkthink.database.mybatis.conditions.Wraps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/subject")
@AllArgsConstructor
@LoginMode(strict = false)
@Api(value = "专题接口", tags = "专题接口")
public class SubjectController {

    private SubjectService subjectService;

    private SubjectProductService subjectProductService;

    @GetMapping({"/querySubjectDetail/{subjectKey}"})
    @ApiOperation("查询专题")
    public R<Subject> querySubjectDetail(@PathVariable String subjectKey) {
        return R.success(subjectService.getOne(Wraps.<Subject>lbQ().eq(Subject::getSubjectKey,subjectKey)));
    }

    @GetMapping({"/querySubjectDetailByKeyList/{subjectKeyList}"})
    @ApiOperation("查询专题列表")
    public R<List<Subject>> querySubjectDetailByKeyList(@PathVariable String subjectKeyList) {
        return R.success(subjectService.list(Wraps.<Subject>lbQ().in(Subject::getSubjectKey,subjectKeyList.split(","))));
    }

    @PostMapping({"/querySubjectProduct"})
    @ApiOperation("分页查询专题商品")
    public R<ProductPageVO> querySubjectProduct(@RequestBody PageParams<SubjectProduct> pageParams) {
        return subjectProductService.querySubjectDetail(pageParams);
    }

    @PostMapping({"/querySubjectWithProduct/{subjectKey}"})
    @ApiOperation("查询专题主信息及专题商品")
    public R<SubjectProductCombineDTO> querySubjectProduct(@PathVariable("subjectKey") String subjectKey, @RequestBody PageParams<SubjectProduct> pageParams) {
        Subject subject = subjectService.getOne(Wraps.<Subject>lbQ().eq(Subject::getSubjectKey,subjectKey));
        pageParams.getModel().setSubjectId(subject.getId());
        ProductPageVO productPageVO = subjectProductService.querySubjectDetail(pageParams).getData();
        return R.success(new SubjectProductCombineDTO(subject,productPageVO));
    }
}
